package org.example;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;


public class CodeChallengeManager {
    private CodeChallenge currentChallenge;
    private CodeRebirthGame game;

    public CodeChallengeManager(CodeRebirthGame game) {
        this.game = game;
    }

    private static final List<Supplier<CodeChallenge>> BASIC_CHALLENGES = Arrays.asList(
            () -> new SyntaxFixChallenge("修复System.out.println()语句"),
            () -> new LoopChallenge("编写1到10的循环")
    );

    public CodeChallenge getCurrentChallenge() {
        return currentChallenge;
    }

    public void generateNewChallenge() {
        int chapter = game.getPlayerData().getStoryProgress();

        switch(chapter) {
            case 0: // 第一章：基础修复
                currentChallenge = new SyntaxFixChallenge(
                        "修复系统I/O模块\n缺失的分号导致输出功能失效");
                break;

            case 1: // 第二章：核心算法
                currentChallenge = new AlgorithmChallenge(
                        "实现紧急排序算法\n确保内存管理功能正常");
                break;

            case 2: // 第三章：宿主连接
                currentChallenge = new NetworkChallenge(
                        "建立安全连接\n规避主神防火墙检测");
                break;

            case 3: // 第四章：系统渗透
                currentChallenge = new ExploitChallenge(
                        "利用缓冲区溢出漏洞\n获取子系统权限");
                break;

            case 4: // 最终章：主神决战
                currentChallenge = new FinalChallenge(
                        "注入终极病毒代码\n目标：主神核心");
                break;

            default:
                currentChallenge = BASIC_CHALLENGES.get(
                        new Random().nextInt(BASIC_CHALLENGES.size())).get();
        }
    }

    public boolean submitSolution(String code) {
        return currentChallenge.validateSolution(code);
    }
}


